/*
 * @Author: fangzhi 
 * @Date: 2018-04-18 16:09:16 
 * @Last Modified by: fangzhi
 * @Last Modified time: 2018-09-17 15:53:20
 */

 // 工具类 放置各种通用函数
var Tools = {
    // 判断是否为数组 
    isArray(obj) {
        return Object.prototype.toString.call(obj) === '[object Array]';
    },
    // 删除数组最后一个元素
    truncate(arr) {
        var m = arr.slice(0);
        m.splice(m.length-1,1);
        return m;
    },
    // 对象深拷贝
    cloneObj(obj) { 
        var str, newobj = obj.constructor === Array ? [] : {};
        if (typeof obj !== 'object') {
            return;
        } else if (window.JSON) {
            str = JSON.stringify(obj), //系列化对象
                newobj = JSON.parse(str); //还原
        } else {
            for (var i in obj) {
                newobj[i] = typeof obj[i] === 'object' ?
                    this.cloneObj(obj[i]) : obj[i];
            }
        }
        return newobj;
    },
    // 深度合并对象 类似$.extend() 方法
    extend: (function() {
        var copyIsArray,
            toString = Object.prototype.toString,
            hasOwn = Object.prototype.hasOwnProperty,
            class2type = { '[object Boolean]': 'boolean', '[object Number]': 'number', '[object String]': 'string', '[object Function]': 'function', '[object Array]': 'array', '[object Date]': 'date', '[object RegExp]': 'regExp', '[object Object]': 'object' },
            type = function(obj) { return obj == null ? String(obj) : class2type[toString.call(obj)] || "object"; },
            isWindow = function(obj) { return obj && typeof obj === "object" && "setInterval" in obj; },
            isArray = Array.isArray || function(obj) { return type(obj) === "array"; },
            isPlainObject = function(obj) {
                if (!obj || type(obj) !== "object" || obj.nodeType || isWindow(obj)) { return false; }
                if (obj.constructor && !hasOwn.call(obj, "constructor") && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) { return false; }
                var key;
                for (key in obj) {}
                return key === undefined || hasOwn.call(obj, key);
            },
            extend = function(deep, target, options) {
                for (name in options) {
                    let src = target[name],
                        copy = options[name],
                        clone = null;
                    if (target === copy) { continue; }
                    if (deep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)))) {
                        if (copyIsArray) {
                            copyIsArray = false;
                            clone = src && isArray(src) ? src : [];
                        } else { clone = src && isPlainObject(src) ? src : {}; }
                        target[name] = extend(deep, clone, copy);
                    } else if (copy !== undefined) { target[name] = copy; }
                }
                return target;
            };
        return extend;
    })(),
    
}

export default Tools;